<!-- 分配工单 -->
<template>
  <a-modal
    title='指派工单'
    :width='740'
    :visible='visible'
    :confirmLoading='loading'
    v-bind='!edit ? {footer: null} :{}'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <div style='max-height: 500px;overflow: auto'>
      <a-spin :spinning='loading'>

        <a-form :form='form' v-bind='formLayout'>
          <a-form-item label='主键' v-show='false'>
            <a-input :disabled='!edit' v-decorator="['id']" />
          </a-form-item>

          <a-form-item label='接单人'>
            <a-select placeholder='请选择接单人'
                      show-search
                      :filterOption='filterOptionFun'
                      :options='userList'
                      v-decorator="['userName', {rules:[{  required: true, message: '请选择接单人!' }]}]">
            </a-select>
          </a-form-item>

          <a-form-item label='备注'>
            <a-textarea placeholder='请输入备注信息'
                        v-decorator="['desc']" />
          </a-form-item>

        </a-form>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import {  phonePattern } from '@/reges'
import { findDataDicInfoByType } from '@/api/dataDicInfo'
import { findBypageEquipment } from '@/api/equipmentManagement'
import api, { downloadAvatar } from '@/api/workerOrder'
import TagSelectOption from '@/components/TagSelect/TagSelectOption'
import { mapState } from 'vuex'
import { ACCESS_TOKEN } from '@/store/mutation-types'
// 表单字段
const fields = ['id', 'userName', 'desc']
import { findAllUserByPermissionCode } from '@/api/user'

let baseUrl = window.PROJECT_CONFIG.VUE_APP_API_BASE_URL
export default {
  components: { TagSelectOption },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    edit: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapState({
      token: state => state.user.token
    }),
    computedHeaders() {
      return {
        [ACCESS_TOKEN]: `Bearer ${this.token}`
      }
    },
    currentTitle() {
      let title = ''
      if (this.edit && (!this.model || !this.model.id)) {
        title = '新增缺陷'
      }
      if (this.edit && this.model && this.model.id) {
        title = '编辑缺陷'
      }
      if (!this.edit) {
        title = '缺陷详情'
      }
      return title
    }
  },
  data() {
    this.baseUrl = baseUrl
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      form: this.$form.createForm(this),
      queryParam: {},
      phonePattern,
      userList: [],
      defectLevelDicList: [],
      uploadPath: baseUrl + api.uploadWorkOrderPhoto,
      stationList: [],
      deviceList: [],
      systemId: '',
      imageUrl: '',
      uploadLoading: false
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    this.findAllUserByPermissionCode()
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    filterOptionFun (inputValue, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(inputValue.toLowerCase()) >= 0
      )
    },
    findAllUserByPermissionCode() {
      findAllUserByPermissionCode({ pageNum: 1, pageSize: 999, permissionCode: 'yunweiguanli' }).then(res => {
        this.userList = res.map(item => {
          return {
            ...item,
            key: item.username,
            label: item.username
          }
        })
      })
    },
    handleChange(info) {
      if (info.file.status === 'uploading') {
        this.uploadLoading = true
        return
      }
      if (info.file.status === 'done') {
        this.imageUrl = downloadAvatar({ fileName: info.file.response.result })
        this.uploadLoading = false
      }
    },
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/JEPG'
      if (!isJpgOrPng) {
        this.$message.error('仅支持JPEG格式以及PNG格式文件!')
      }
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('图片大小不得超过 10MB!')
      }
      return isJpgOrPng && isLt2M
    },
    normFile(e) {
      if (Array.isArray(e)) {
        return e
      }
      if (e.fileList && e.fileList.length) {
        return e && e.fileList[0].response && e.fileList[0].response.result
      }
      return ''
    }
  }
}
</script>
<style lang='less' scoped>
.ant-modal-body {
  height: 700px;
  overflow: auto;
}

.avatar {
  width: 200px;
  height: 200px;
}
</style>
